package com.stadsetjari_hi.transmissionclasses;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.stadsetjari_hi.Main;
import com.stadsetjari_hi.R;

public class Stadsetjari 
{	
	public static void TransmitLocationData(Context maincontext)
	{
		SharedPreferences preferences = maincontext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
		setNotification(maincontext);
		TransmitDataBroadcastReciever.SetAlarm(maincontext, preferences.getInt("intervalTransmit", 1));
		preferences.edit().putBoolean("Tstate", true).commit();
	}
	public static void stopTransmittingLocationData(Context maincontext)
	{
		SharedPreferences preferences = maincontext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
		cancelNotification(maincontext);
		TransmitDataBroadcastReciever.CancelAlarm(maincontext);
		preferences.edit().putBoolean("Tstate", false).commit();
	}
	public static void LogginUser(String Username,String Password)
	{
		
	}
	public static void RegisterUser(String Username,String Password)
	{
		
	}
	public static boolean isTransmitting(Context maincontext)
	{
		SharedPreferences preferences = maincontext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
		return preferences.getBoolean("Tstate", false);
	}
	public static boolean isLoggedIn(Context maincontext)
	{
		SharedPreferences preferences = maincontext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
		return preferences.getBoolean("savedUserInfo", false);
	}
	public static void LogOutUser(Context maincontext)
	{
		SharedPreferences preferences = maincontext.getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
		preferences.edit().clear().commit();
	}
	
	
	@SuppressLint("NewApi")
	private static void setNotification(Context maincontext)
  	{
		NotificationManager notificationManager = (NotificationManager) maincontext.getSystemService(Activity.NOTIFICATION_SERVICE);
  		Intent intent = new Intent(maincontext, Main.class);
        PendingIntent pIntent = PendingIntent.getActivity(maincontext, 0, intent, 0);
        
        Notification noti = new Notification.Builder(maincontext)
        .setContentTitle("Transmitting Location Data")
        .setContentText("Online")
        .setSmallIcon(R.drawable.kub2)
        .setContentIntent(pIntent)
        .build();

        noti.flags |= Notification.FLAG_ONGOING_EVENT;

        notificationManager.notify(0, noti);	
  	}
	private static void cancelNotification(Context maincontext)
	{
		NotificationManager notificationManager = (NotificationManager) maincontext.getSystemService(Activity.NOTIFICATION_SERVICE);
  		notificationManager.cancel(0);
	}
}
